#!/usr/bin/python3.7
# -*- coding: utf-8 -*-
# @Time    : 2021/3/7 11:21 下午
# @Author  : 白辉东
# @Email   : xyxk-huidong.bai@ximalaya.com
# @File    : conftest.py
# @Software: PyCharm
import threading
from public.linuxLogCatchTools import write_log
from public.linuxLogCatchTools import clear_cash_log
import pytest


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    print('------------------------------------')
    # 获取钩子方法的调用结果
    out = yield

    # 从钩子方法的调用结果中获取测试报告
    # 钩子函数和case后置函数到底是谁先执行？
    report = out.get_result()
    if report.when == "call":
        print('测试报告：%s' % report)
        print('步骤：%s' % report.when)
        print('nodeid：%s' % report.nodeid)
        print('description:%s' % str(item.function.__doc__))
        print(('运行结果: %s' % report.outcome))

        if report.outcome == "failed":
            # TODO 将失败的日志存入failed_log中
            # 注意这里如果reruns，以覆盖的形式存放，获取相关的query和sn或者speaker_type
            print('将失败的日志存入失败文件夹中')
            pass


@pytest.fixture(scope='function', autouse=True, name='t_begin')
def function_fixture(all_fixture):
    print('函数前置')
    sn, speaker_type = all_fixture
    t = threading.Thread(target=write_log, args=(sn, speaker_type))
    t.start()
    yield t
    # 清空日志
    clear_cash_log(speaker_type)
    print('函数后置')
